﻿@using System.ComponentModel.DataAnnotations
@using Authority.HttpManage
@using Authority.PresentationModel.User
@using Materal.Common
@using Materal.Model
@using WebAPP.MateralUI.Models

@inject MessageManage _messageManage
@inject ExceptionManage _exceptionManage
@inject IUserManage _userManage

<section>
    <MForm @ref="@Form" FromData="@FormData">
        <MFormItem Title="旧密码" PropertyModel="@context.OldPassword" IsRequired>
            <MInput Type="password" Disabled="@Loading" @bind-Value="@context.OldPassword.Value"></MInput>
        </MFormItem>
        <MFormItem Title="新密码" PropertyModel="@context.NewPassword" IsRequired>
            <MInput Type="password" Disabled="@Loading" @bind-Value="@context.NewPassword.Value"></MInput>
        </MFormItem>
        <MFormItem Title="重复新密码" PropertyModel="@context.RepeatPassword" IsRequired>
            <MInput Type="password" Disabled="@Loading" @bind-Value="@context.RepeatPassword.Value"></MInput>
        </MFormItem>
    </MForm>
    <div class="m_modal_buttons">
        <MButton Disabled="@Loading" OnClick="@OnBtnCancelClickAsync">取消</MButton>
        <MButton ButtonType="@MButtonTypeEnum.Primary" Disabled="@Loading" OnClick="@OnBtnChangePasswordClickAsync">确定</MButton>
    </div>
</section>
@code{
    /// <summary>
    /// 更改密码表单数据模型
    /// </summary>
    public class ChangePasswordFromDataModel
    {
        /// <summary>
        /// 旧密码
        /// </summary>
        [Required(ErrorMessage = "旧密码必填")]
        public FromDataPropertyModel<string> OldPassword { get; set; } = new FromDataPropertyModel<string>();
        /// <summary>
        /// 新密码
        /// </summary>
        [Required(ErrorMessage = "新密码必填")]
        public FromDataPropertyModel<string> NewPassword { get; set; } = new FromDataPropertyModel<string>();
        /// <summary>
        /// 重复新密码
        /// </summary>
        [Required(ErrorMessage = "重复新密码必填")]
        public FromDataPropertyModel<string> RepeatPassword { get; set; } = new FromDataPropertyModel<string>();
    }
}
@code {
    #region 参数
    /// <summary>
    /// 更改成功
    /// </summary>
    [Parameter]
    public EventCallback ChangeSuccess { get; set; }
    /// <summary>
    /// 取消
    /// </summary>
    [Parameter]
    public EventCallback Cancel { get; set; }
    #endregion
    #region 属性字段
    /// <summary>
    /// 加载状态
    /// </summary>
    public bool Loading { get; set; }
    /// <summary>
    /// 窗体
    /// </summary>
    public MForm<ChangePasswordFromDataModel> Form { get; set; }
    /// <summary>
    /// 表单数据
    /// </summary>
    public ChangePasswordFromDataModel FormData = new ChangePasswordFromDataModel();
    #endregion
    #region 事件
    /// <summary>
    /// 单击更改密码按钮
    /// </summary>
    /// <param name="eventArgs"></param>
    /// <returns></returns>
    public async Task OnBtnChangePasswordClickAsync(MouseEventArgs eventArgs)
    {
        await ChangePasswordAsync();
    }
    /// <summary>
    /// 单击更改密码按钮
    /// </summary>
    /// <param name="eventArgs"></param>
    /// <returns></returns>
    public async Task OnBtnCancelClickAsync(MouseEventArgs eventArgs)
    {
        await Cancel.InvokeAsync(null);
    }
    #endregion
    #region 私有方法
    /// <summary>
    /// 更改密码
    /// </summary>
    /// <returns></returns>
    private async Task ChangePasswordAsync()
    {
        if (!Form.ValidateData()) return;
        if (FormData.NewPassword.Value != FormData.RepeatPassword.Value)
        {
            FormData.NewPassword.CanError = true;
            FormData.NewPassword.ErrorMessage = "两次输入的密码必须相同";
            FormData.RepeatPassword.CanError = true;
            FormData.RepeatPassword.ErrorMessage = "两次输入的密码必须相同";
            return;
        }
        Loading = true;
        try
        {
            var requestModel = new ChangePasswordRequestModel
            {
                OldPassword = FormData.OldPassword.Value,
                NewPassword = FormData.NewPassword.Value
            };
            ResultModel resultModel = await _userManage.ChangePasswordAsync(requestModel);
            if (resultModel.ResultType == ResultTypeEnum.Success)
            {
                await _messageManage.SuccessAsync(resultModel.Message);
                await ChangeSuccess.InvokeAsync(null);
            }
            else
            {
                await _messageManage.WarningAsync(resultModel.Message);
            }
        }
        catch (Exception exception)
        {
            await _exceptionManage.HandlerExceptionAsync(exception);
        }
        finally
        {
            Loading = false;
        }
    }
    #endregion
}
